﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var SGoodsMoveID = $.trim($("#txtSGoodsMoveID").attr("value"));
    var SGoodsBatchCode = $.trim($("#txtSGoodsBatchCode").attr("value"));
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/GoodsMove/QueryGoodsMoveItem',
        title: '下架指令列表',
        queryParams: {
            SGoodsMoveID: SGoodsMoveID,
            SGoodsBatchCode: SGoodsBatchCode
        },
        //width: 815,
        height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'SEQNO',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'SEQNO', title: '明细序列ID', hidden: true },
                    { field: 'GOODSMOVEID', title: '搬移单号', align: 'center', sortable: true },
                    { field: 'GOODSBATCHCODE', title: '商品批次号', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '商品名称', align: 'center', sortable: true },
                    { field: 'FROMLOCATIONCODE', title: '来源货位', align: 'center', sortable: true },
                    { field: 'TOLOCATIONCODE', title: '目的货位', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '商品编码', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '商品名称', align: 'center', sortable: true },
                    { field: 'QTY', title: '数量', align: 'center', sortable: true },
                    { field: 'OFFSHELVESID', title: '下架指令ID', align: 'center', sortable: true }
        ]],
        toolbar: [{
            id: 'btnAdd',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }, '-', {
            id: 'btnRemove',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}

function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }

    return "";
}

function formatflag(val) {
    if (val != null) {
        if (val == 0) {
            return "未处理";
        }
        if (val == 40) {
            return "在执行";
        }
        if (val == 100) {
            return "完结";
        }
    }
}

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');

    //$('#btnImport').linkbutton('disable');
    // $('#btnExport').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
        $('#btnRemove').linkbutton('enable');
    }
}


//增加
function add() {
    $('#p1').dialog('open').dialog('setTitle', '新增搬移明细信息');
    $("#hiddenSEQNO").val("");
    $("#txtGoodsMoveID").val("");
    $("#txtGoodsBatchCode").val("");
    $("#txtFromLocationCode").val("");
    $("#txtToLocationCode").val("");
    $("#txtQTY").val("");
    $("#txtOffShelvesID").val("");
    url = '/GoodsMove/SaveItems';
}

//修改
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#p1').dialog('open').dialog('setTitle', '修改搬移明细信息');
    $("#hiddenSEQNO").val(rows[0].SEQNO);
    $("#txtGoodsMoveID").val(rows[0].GOODSMOVEID);
    $("#txtGoodsBatchCode").val(rows[0].GOODSBATCHCODE);
    $("#txtFromLocationCode").val(rows[0].FROMLOCATIONCODE);
    $("#txtToLocationCode").val(rows[0].TOLOCATIONCODE);
    $("#txtQTY").val(rows[0].QTY);
    $("#txtOffShelvesID").val(rows[0].OFFSHELVESID);
    url = '/GoodsMove/SaveItems';

}



//删除
function del() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].OFFSHELVESID);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            $('#location').form('submit', {
                url: '/GoodsMove/DeleteItem' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGrid();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}


function save() {
    var SEQNO = $.trim($("#hiddenSEQNO").attr("value"));
    var GoodsMoveID = $.trim($("#txtGoodsMoveID").attr("value"));
    var GoodsBatchCode = $.trim($("#txtGoodsBatchCode").attr("value"));
    var FromLocationCode = $.trim($("#txtFromLocationCode").attr("value"));
    var ToLocationCode = $.trim($("#txtToLocationCode").attr("value"));
    var QTY = $.trim($("#txtQTY").attr("value"));

    if (GoodsMoveID == "") {
        $.messager.alert('提示', '搬移单号不能为空', 'warning');
        $("#txtGoodsMoveID").focus();
        return false;
    }
    if (GoodsBatchCode == "") {
        $.messager.alert('提示', '商品批次号不能为空', 'warning');
        $("#txtGoodsBatchCode").focus();
        return false;
    }
    if (QTY == "") {
        $.messager.alert('提示', '数量不能为空', 'warning');
        $("#txtQTY").focus();
        return false;
    }
    if (FromLocationCode == "") {
        $.messager.alert('提示', '来源货位不能为空', 'warning');
        $("#txtFromLocationCode").focus();
        return false;
    }
    if (ToLocationCode == "") {
        $.messager.alert('提示', '目的货位不能为空', 'warning');
        $("#txtToLocationCode").focus();
        return false;
    }


    $('#location').form('submit', {
        url: url + '?SEQNO=' + SEQNO,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p1').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}


//提交参数
function submitParams() {
    $('#fm2').form('submit', {
        url: '/Distr/UpdateParams',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                grid.datagrid('reload');
                $.messager.alert('提示', '参数修改成功!', 'info');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

